package com.owen.server.impl;

import com.owen.dao.CommodityDao;
import com.owen.pojo.Commodity;
import com.owen.pojo.Fenlei;
import com.owen.server.CommodityServer;
import com.owen.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityServerImpl implements CommodityServer {

    @Autowired
    private CommodityDao commodityDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Commodity> findAll() {
        List<Commodity> commodities = (List<Commodity>)redisUtil.get("findCommodity");
        if(commodities == null){
            commodities = commodityDao.findAll();
            redisUtil.set("findCommodity", commodities);
        }
        return commodities;
    }

    @Override
    public List<Commodity> findByCommodity(Commodity commodity) {
        return commodityDao.findByCommodity(commodity);
    }

    @Override
    public List<Commodity> findByCommodityByFenlei(Commodity commodity, Long[] fenleiids) {
        return commodityDao.findByCommodityByFenlei(commodity, fenleiids);
    }

    @Override
    public Commodity findById(Integer id) {
        return commodityDao.findById(id);
    }

    @Override
    public Integer deleteBatch(Integer[] ids) {
        Integer integer = commodityDao.deleteBatch(ids);
        if(integer > 0){
           redisUtil.del("findCommodity");
        }
        return integer;
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer integer = commodityDao.deleteById(id);
        if(integer > 0){
            redisUtil.del("findCommodity");
        }
        return integer;
    }

    @Override
    public Integer updateById(Commodity commodity) {
        Integer integer = commodityDao.updateById(commodity);
        if(integer > 0){
            redisUtil.del("findCommodity");
        }
        return integer;
    }

    @Override
    public Integer addCommodityFenlei(Integer commodityId, Integer fenleiId) {
        Integer integer = commodityDao.addCommodityFenlei(commodityId, fenleiId);
        if(integer > 0){
            redisUtil.del("findCommodity");
        }
        return integer;
    }

    @Override
    public Integer deleteCommodityFenleiById(Integer id) {
        Integer integer = commodityDao.deleteCommodityFenleiById(id);
        if(integer > 0){
            redisUtil.del("findCommodity");
        }
        return integer;
    }

    @Override
    public Integer addCommodityById(Commodity commodity) {
        Integer integer = commodityDao.addCommodityById(commodity);
        if(integer > 0){
            redisUtil.del("findCommodity");
        }
        return integer;
    }

}